

*Assign the directory for the table + figure output
global tnf .\TablesAndFigures\


********************************************************************************
* Table 7 - Summary Stats for the Auto Loan Sample
********************************************************************************
* The code below writes output that can easily be pasted into a LaTex table.  
use Data_AutoLoans, clear // synthetic data created in _0_GenerateSyntheticData.do

local varlist sex_female age hmda_app_income d_delinqf2_aua apr aua_credit_open lti_aua aua_termofrecenttrade l1_vantage_v3_score l1_all_balance_open new_dti l1_all_pastdue2 aua_debtshare_all

*define samples (columns 1-5 of Table 7)
local moreif 
local ifcondition1 ""
local ifcondition2 "if race_minority==0 `moreif'"
local ifcondition3 "if race_minority==1 `moreif'"
local ifcondition4 "if l1_subprime==1 `moreif'"
local ifcondition5 "if l1_subprime==0 `moreif'"

count `ifcondition1'
count `ifcondition2'
count `ifcondition3'
count `ifcondition4'
count `ifcondition5'

local n: word count `varlist'
forvalues i = 1/`n' {
    local var : word `i' of `varlist'
	
	forvalues s=1/5 {
		qui summ `var' `ifcondition`s'', detail
		local mean`s' `r(mean)'
		local sd`s' `r(sd)'	
		}
	
	if `i'==1 {
		di "" _col(30) " & mean(g1)" _col(40) " & mean(g2)" _col(50) " & mean(g3)" _col(60) "  & mean(g4)" _col(70) "  & mean(g5)   \\"
		}
	di " `:var label `var''" _col(30) " & `:di %12.3g `mean1''" _col(40) " & `:di %12.3g `mean2''" _col(50) " & `:di %12.3g `mean3''" _col(60)  " & `:di %12.3g `mean4''" _col(70) " & `:di %12.3g `mean5'' \\"
	di " "    " & \begin{scriptsize}[`:di %12.3g `sd1'']\end{scriptsize}"    " & \begin{scriptsize}[`:di %12.3g `sd2'']\end{scriptsize}"      " & \begin{scriptsize}[`:di %12.3g `sd3'']\end{scriptsize}"      " & \begin{scriptsize}[`:di %12.3g `sd4'']\end{scriptsize}"     " & \begin{scriptsize}[`:di %12.3g `sd5'']\end{scriptsize} \\"
	}

	
	
********************************************************************************
* Table 8 - Effect of borrower race on auto loan interest rates
********************************************************************************
use Data_AutoLoans, clear // synthetic data created in _0_GenerateSyntheticData.do

global y apr
global democontrols sex_female age ln_hmda_app_income
global autocontrols ln_aua_credit_open lti_aua   	   
global creditscore l1_vantage_v3_score
global creditcontrols ln_l1_all_balance_open new_dti ln_l1_all_pastdue2 aua_debtshare_all    	   
global zipcontrols ln_acs_pipc ln_popdensity acs_edu_bagrad acs_commute_usecar 
global FE aua_termofrecenttrade timetomort statefips#year origmonth

estimates clear
local ifcond
*build up to full set of controls
eststo: reghdfe ${y} race_minority ${democontrols} ${autocontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year)  
		
eststo: reghdfe ${y} race_minority ${democontrols} ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year) 			

*interact w racial bias
eststo: reghdfe ${y} race_minority race_minorityXbias ${democontrols} ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year) 		

*interact w low banking competition
eststo: reghdfe ${y} race_minority race_minorityXlowcomp lowcomp ${democontrols} ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year) 		
		
*interact w low dealer competition
eststo: reghdfe ${y} race_minority race_minorityXlowdealercomp lowdealercomp ${democontrols} ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year) 			

*interact w high non-bank financing
eststo: reghdfe ${y} race_minority race_minorityXnonbank_hi nonbank_hi ${democontrols} ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year) 	
		
*interact w rural
eststo: reghdfe ${y} race_minority race_minorityXrural rural ${democontrols} ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond', absorb(${FE}) cluster(statefips#year) 			
	
esttab _all using ${tnf}T_InterestRates ,title("") ///
	replace tex frag ///
	order(race_minority race_minorityXbias race_minorityXlowcomp lowcomp race_minorityXlowdealercomp lowdealercomp race_minorityXnonbank_hi nonbank_hi race_minorityXrural rural ${democontrols} ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols}) noomitted  b(%12.3f) se(%12.3f) transform(100*@ 100)   ///
	stats(r2 N, fmt(%12.3f %12.0gc) labels("R-Squared" "Observations")) se star(* .10 ** .05 *** .01) ///
	label nogaps nonumbers 


	
********************************************************************************	
* Table 9 - Borrower race and auto loan default rates
********************************************************************************
*note: continue using the same data and globals defined for Table 8.		

*this illustrates that the defaults analysis sample is smaller - see Section 3.3.2 for the details.
gen z = runiform(0,1)
gen defsample = 0 
replace defsample =1 if z>=.5
drop z
	
	
estimates clear
*now set y as the default indicator
	global y d_delinqf2_aua 
*restrict to the defaults sample
	local ifcond if defsample==1 
	
*full sample	   	
eststo: reghdfe ${y} race_minority ${democontrols} apr ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond' , absorb(${FE}) cluster(statefips#year) 			
*subprime subsample	
eststo: reghdfe ${y} race_minority ${democontrols} apr ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond' & l1_subprime==1 , absorb(${FE}) cluster(statefips#year) 			
*prime subsample
eststo: reghdfe ${y} race_minority ${democontrols} apr ${autocontrols} ${creditscore} ${creditcontrols} ${zipcontrols} ///
        `ifcond' & l1_subprime==0 , absorb(${FE}) cluster(statefips#year) 			

esttab _all using ${tnf}T_DefaultRates  ,title("") ///
	replace tex frag ///
	order(race_minority ${democontrols} ${autocontrols} apr ${creditscore} ${creditcontrols} ${zipcontrols}) noomitted  b(%12.3f) se(%12.3f) transform(100*@ 100)   ///
	stats(r2 N, fmt(%12.3f %12.0gc) labels("R-Squared" "Observations")) se star(* .10 ** .05 *** .01) ///
	label nogaps nonumbers ///
	mgroups("Full Sample" "Subprime Borrowers" "Prime Borrowers", pattern(0 1 1)  span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span}))  	
	
  
  
  
  
